package com.cardone.platform.usercenter.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.usercenter.dto.UsOrgTranslateDto;

/**
 * 用户组织调动
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class UsOrgTranslateJdbcDao implements UsOrgTranslateDao {
	@Override
	public int deleteByIds(final UsOrgTranslateDto deleteUsOrgTranslate) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteUsOrgTranslate.getAttrs())) {
			model.putAll(deleteUsOrgTranslate.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteUsOrgTranslate.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final UsOrgTranslateDto findUsOrgTranslate) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsOrgTranslate, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findUsOrgTranslate, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findUsOrgTranslate.getAttrs())) {
			model.putAll(findUsOrgTranslate.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final UsOrgTranslateDto findUsOrgTranslate) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsOrgTranslate);

		final Model model = ModelUtils.newModel(new ModelArgs(findUsOrgTranslate, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findUsOrgTranslate.getAttrs())) {
			model.putAll(findUsOrgTranslate.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final UsOrgTranslateDto findListUsOrgTranslate) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListUsOrgTranslate).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListUsOrgTranslate.getAttrs())) {
			model.putAll(findListUsOrgTranslate.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final UsOrgTranslateDto insertUsOrgTranslate) {
		if (StringUtils.isBlank(insertUsOrgTranslate.getId())) {
			insertUsOrgTranslate.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertUsOrgTranslate));

		if (MapUtils.isNotEmpty(insertUsOrgTranslate.getAttrs())) {
			model.putAll(insertUsOrgTranslate.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final UsOrgTranslateDto paginationUsOrgTranslate) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationUsOrgTranslate).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationUsOrgTranslate.getAttrs())) {
			model.putAll(paginationUsOrgTranslate.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationUsOrgTranslate), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final UsOrgTranslateDto readUsOrgTranslate) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readUsOrgTranslate, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readUsOrgTranslate.getAttrs())) {
			model.putAll(readUsOrgTranslate.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readUsOrgTranslate);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readUsOrgTranslate, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final UsOrgTranslateDto saveUsOrgTranslate) {
		final P oldUsOrgTranslate = this.findByCodeOrId(mappedClass, saveUsOrgTranslate);

		if (oldUsOrgTranslate == null) {
			this.insertByCode(saveUsOrgTranslate);

			return this.findByCodeOrId(mappedClass, saveUsOrgTranslate);
		}

		final int updateCount = this.updateByCode(saveUsOrgTranslate, oldUsOrgTranslate);

		if (updateCount < 1) {
			return oldUsOrgTranslate;
		}

		return this.findByCodeOrId(mappedClass, saveUsOrgTranslate);
	}

	@Override
	public int updateByCode(final UsOrgTranslateDto updateUsOrgTranslate) {
		final UsOrgTranslateDto oldUsOrgTranslate = this.findByCodeOrId(UsOrgTranslateDto.class, updateUsOrgTranslate);

		return this.updateByCode(updateUsOrgTranslate, oldUsOrgTranslate);
	}

	private <P> int updateByCode(final UsOrgTranslateDto updateUsOrgTranslate, final P oldUsOrgTranslate) {
		if (oldUsOrgTranslate == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateUsOrgTranslate);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateUsOrgTranslate, oldUsOrgTranslate, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldUsOrgTranslate, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldUsOrgTranslate, updateUsOrgTranslate, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateUsOrgTranslate, updateProperties), new ModelArgs(updateUsOrgTranslate, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateUsOrgTranslate.getAttrs())) {
			model.putAll(updateUsOrgTranslate.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}